//  DATA: 10/24/2022
//  USER: WANGDA
//  FUNCTION:  POWER_SET
// 开机自启动用于音量控制和开关机

#include <stdio.h>
#include <linux/input.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "taiic_key.h"
#include "rk_debug.h"

#include "pthread.h"

#include "test_comm_utils.h"
#include "taiic_uart_sum.h"

#define MAXVOC 30
#define MINVOC 6
#define MAXEVENT 2
#define DEFALUTVOC 24
#define HOLDTIME 3000      // MS
#define HOLDTIME_LONG 5000 // MS
long hold_time = 0;

typedef struct _taiicPowerCtx
{
    /* data */

    pthread_t tidSend[MAXEVENT];

    int fd[MAXEVENT];
    char path[MAXEVENT][64];
    struct input_event t[MAXEVENT];

} TAIIC_POWER_EVENT_S;

TAIIC_POWER_EVENT_S *power_event;
int vocCom;                                                      // 开机默认音量大小
char vocCmd[256] = "rk_mpi_amix_test -C 0 --control 23 --value"; // 指令前部分
char vocCmdFinal[256] = {0};                                     // 完整指令

void *powerDown(void *ptr)
{
    TAIIC_POWER_EVENT_S *params = reinterpret_cast<TAIIC_POWER_EVENT_S *>(ptr);
    snprintf(params->path[1], sizeof(params->path[1]), SYSFS_POWER_DOWN);
    params->fd[1] = open(params->path[1], O_RDONLY | O_NOCTTY);
    int keyCount = 0;
    // 按下按键，音量-
    // while (1)
    for (;;)
    {
        /* code */
        int res = read(params->fd[1], &params->t[1], sizeof(params->t[1]));
        timeval start_us;
        timeval end_us;

        if (res > 0)
        {
            // printf("PowerDown:code==%d,value==%d,type=%d\n",params->t[1].code,params->t[1].value,params->t[1].type);
            if (params->t[1].code == POWER_DOWN)
            {
                if (params->t[1].value == TAIIC_KEY_DOWN) // 1
                {
                    // RK_LOGI("key(%d) is down\n", params->t[1].code);
                    printf("key(%d) is down\n", params->t[1].code);
                    gettimeofday(&start_us, NULL);
                }
                else if (params->t[1].value == TAIIC_KEY_UP) // 0
                {
                    // RK_LOGI("key(%d) is up\n", params->t[1].code);
                    printf("key(%d) is up\n", params->t[1].code);
                    gettimeofday(&end_us, NULL);

                    printf("====hold time is %dms===\n", ((end_us.tv_sec * 1000 + end_us.tv_usec / 1000) - (start_us.tv_sec * 1000 + start_us.tv_usec / 1000)));
                    hold_time = (end_us.tv_sec * 1000 + end_us.tv_usec / 1000) - (start_us.tv_sec * 1000 + start_us.tv_usec / 1000);
                    if (hold_time >= HOLDTIME) // 3000ms=3s
                    {
                        keyCount++;
                        if(keyCount % 2 == 1)
                        {
                            system("killall vsr_taiic_lm_noptt");
                            system("uart_commid 13 0");
                            system("reboot");
                        }
                    }
                    else
                    {
                        // 单击  音量- ，因与拾音降噪功能干扰，去掉该功能。 通过手机去控制耳机喇叭音量大小
                        /*vocCom = ((vocCom = vocCom - 3) <= MINVOC) ? MINVOC : vocCom;
                        snprintf(vocCmdFinal, sizeof(vocCmdFinal), "%s %d", vocCmd, vocCom);
                        system(vocCmdFinal);*/
                    }
                }
                /*else if (params->t[1].value == VOC_KEY_HOLD)
                {
                    keyCount++;

                    RK_LOGI("====key count %d====\n", keyCount);
                    if (keyCount > 20)
                    {
                        RK_LOGI("====power off=====\n");

                        system("poweroff");
                    }
                }*/
            }
        }

        sleep(0.5);
    }
}

int main()
{
    power_event = reinterpret_cast<TAIIC_POWER_EVENT_S *>(malloc(sizeof(TAIIC_POWER_EVENT_S))); // 分配内存空间
    memset(power_event, 0, sizeof(TAIIC_POWER_EVENT_S));                                        // 统一设置为0

    // 开关机按键
    pthread_create(&power_event->tidSend[1], RK_NULL, powerDown, reinterpret_cast<void *>(&power_event));
    pthread_join(power_event->tidSend[1], RK_NULL);
    /*===================================*/

    return 0;
}